package com.weic.chain;

import java.util.ArrayList;
import java.util.List;

public class FilterMain {

    public static void main(String[] args) {
        MyRequest request = new MyRequest("request");
        MyResponse response = new MyResponse("response");

        FilterChain filterChain = new FilterChain();
        filterChain.addFilter(new ScriptFilter()).addFilter(new UrlFilter()).addFilter(new EmjFilter());
        filterChain.doFilter(request,response,filterChain);

        System.out.println(request);
        System.out.println(response);
    }
}

class FilterChain implements Filter {
    List<Filter> filterList = new ArrayList<>();
    int index=0;
    public boolean resultFlag = true;
    public FilterChain addFilter(Filter f){
        filterList.add(f);
        return this;
    }
    public boolean doFilter(MyRequest request,MyResponse response,FilterChain chain){
        if(index == filterList.size() ){
           return false;
        }
        Filter f = filterList.get(index);
        index++;
        resultFlag = f.doFilter(request, response, chain);
        return resultFlag;
    }
}
interface Filter{
    boolean doFilter(MyRequest request,MyResponse response,FilterChain chain);
}
class UrlFilter implements Filter {

    public boolean doFilter(MyRequest request,MyResponse response,FilterChain chain){
        if (!chain.resultFlag){
            return false;
        }
        request.setRequest(request.getRequest()+"<-【UrlFilter】->");
        chain.resultFlag =false;
        chain.doFilter(request, response, chain);
        response.setResponse(response.getResponse()+"<-【UrlFilter】->");
        return false;
    }
}
class EmjFilter implements Filter {
    public boolean doFilter(MyRequest request,MyResponse response,FilterChain chain){
        if (!chain.resultFlag){
            return false;
        }
        request.setRequest(request.getRequest()+"<-【EmjFilter】->");
        chain.doFilter(request,response,chain);
        response.setResponse(response.getResponse()+"<-【EmjFilter】->");
        return true;
    }
}

class ScriptFilter implements Filter {
    public boolean doFilter(MyRequest request,MyResponse response,FilterChain chain){
        if (!chain.resultFlag){
            return false;
        }
        request.setRequest(request.getRequest()+"<-【ScriptFilter】->");
        chain.doFilter(request,response,chain);
        response.setResponse(response.getResponse()+"<-【ScriptFilter】->");
        return true;
    }
}

class MyResponse{
    private String response;

    public MyResponse(String response) {
        this.response = response;
    }

    public String getResponse() {
        return response;
    }

    public void setResponse(String response) {
        this.response = response;
    }

    @Override
    public String toString() {
        return "MyResponse{" +
                "response='" + response + '\'' +
                '}';
    }
}
class MyRequest {
    private String request;

    public MyRequest(String msg) {
        this.request = msg;
    }

    public String getRequest() {
        return request;
    }

    public void setRequest(String request) {
        this.request = request;
    }

    @Override
    public String toString() {
        return "MyRequest{" +
                "request='" + request + '\'' +
                '}';
    }
}